package everyday;

import java.util.Comparator;
import java.util.HashMap;
import java.util.stream.IntStream;

/**
 * @Author: Siantar
 * @Date: 2023-08-15-17:27
 * @Description: 1.0
 */
public class T0833 {
    public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
        int[] order = IntStream.range(0, indices.length).boxed().sorted(Comparator.comparingInt(i -> indices[i]))
                .mapToInt(i -> i).toArray();
        StringBuilder sb = new StringBuilder();
        int preIndex = 0;
        for (int i : order) {
            int index = indices[i];
            sb.append(s, preIndex, index);
            if (s.startsWith(sources[i], index)) {
                sb.append(targets[i]);
                preIndex = index + sources[i].length();
            } else {
                preIndex = index;
            }
        }
        sb.append(s.substring(preIndex));
        return sb.toString();
    }
}
